/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing.text;

import java.util.*;
 
import javax.swing.*;

import jdbcmanager.dbtools.*;

/**
 * A text editor that contains specific functionality for SQL language.
 * 
 * @author Francisco Morero Peyrona
 */
public final class JxSQLCodePane extends JxCodePane
{
   /**
    * Class constructor.
    */
   public JxSQLCodePane()
   {  
      SQLStyledDocument docSQL  = new SQLStyledDocument(); 
      Hashtable         htFuncs = DbUtil.getAgentDBMS().getAgentMetaData().getPrebuiltFunctions();
      Vector            vFuncs  = new Vector();
      String[]          asFunc;
      
      for( Enumeration e = htFuncs.keys(); e.hasMoreElements(); )
      {
         String[] as = (String[]) htFuncs.get( e.nextElement() );

         for( int n = 0; n < as.length; n++ )
            vFuncs.add( as[n] );
      }
      
      asFunc = new String[ vFuncs.size() ];
      vFuncs.copyInto( asFunc );
      
      docSQL.addBuiltFunctionKeywords( asFunc );
      docSQL.addExtraSQLKeywords( DbUtil.getAgentDBMS().getAgentMetaData().getExtraSQLKeywords() );
      
      setDocument( docSQL );
   }
   
   /**
    * Retrieves text in the edior.
    * 
    * @return The text in the edior
    */
   public String getText()
   {
      String sText = super.getText();
      
      return (sText == null) ? "" : sText.trim().replace( '\n', ' ' ).replace( '\t', ' ' );
   }

   /**
    * Adds passed action to the popup menu.
    * If the action == <code>null</code> a separator is added.
    * 
    * @param action Action to add or <code>null</code> to add a separator.
    */
   public void addAction2Popup( Action action )
   {
      if( action == null )
         popupMenu.addSeparator();
      else
         popupMenu.add( action );
   }
}